package com.javaweb.demo.dao;

import com.javaweb.demo.entity.User;
import com.javaweb.demo.util.DBconn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaolmpl implements UserDao {
    @Override
    public boolean login(String name, String pwd) {
        boolean flag = false;
        try {
            DBconn.init();
            ResultSet rs = DBconn.selectSql("select * from user where name='" + name + "' and pwd='" + pwd + "'");
            while (rs.next()) {
                if (rs.getString("name").equals(name) && rs.getString("pwd").equals(pwd)) {
                    flag = true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean register(User user) {
        try {
            DBconn.init();
            ResultSet rs = DBconn.selectSql("select * from user where name='" + user.getName() + "'");
            if (rs.next()) {
                DBconn.closeConn();
                return false;
            } else {
                int i = DBconn.addUpdDel("insert into user(name,pwd,sex,home,info) " +
                        "values('" + user.getName() + "','" + user.getPwd() + "','" + user.getSex() + "','" + user.getHome() + "','" + user.getInfo() + "')");
                if (i > 0) {
                    DBconn.closeConn();
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBconn.closeConn();
        return false;
    }

    @Override
    public List<User> getUserAll() {
        List<User> list = new ArrayList<>();
        try {
            DBconn.init();
            ResultSet rs = DBconn.selectSql("select * from user");
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPwd(rs.getString("pwd"));
                user.setSex(rs.getString("sex"));
                user.setHome(rs.getString("home"));
                user.setInfo(rs.getString("info"));
                list.add(user);
            }
            DBconn.closeConn();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean update(int id, String name, String pwd, String sex, String home, String info) {
        boolean flag = false;
        DBconn.init();
        String sql = "update user set name ='" + name
                + "' , pwd ='" + pwd
                + "' , sex ='" + sex
                + "' , home ='" + home
                + "' , info ='" + info + "' where id = " + id;
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean delete(String name) {
        boolean flag = false;
        DBconn.init();
        String sql = "delete from user where name = '" + name + "'";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        sql = "delete from friend where (username = '" +  name + "') or (name = '" + name + "')";
        i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public List<User> selectUser(int id,String name,String sex,String home) {
        List<User> list = new ArrayList<>();
        try {
            DBconn.init();
            String sql = "select * from user where (name = '" + name + "') or (name = '" + name + "' and sex = '" + sex + "' and home = '" + home + "' ) or id =" + id;
            ResultSet rs = DBconn.selectSql(sql);
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setSex(rs.getString("sex"));
                user.setHome(rs.getString("home"));
                user.setInfo(rs.getString("info"));
                list.add(user);
            }
            DBconn.closeConn();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean addfriend(String username,String name,String request) {
        try {
            DBconn.init();
            ResultSet rs = DBconn.selectSql("select * from friend where username='" + username +
                    "' and name='" + name + "'");
            if (rs.next()) {
                DBconn.closeConn();
                return false;
            } else {
                int i = DBconn.addUpdDel("insert into addfriendrequest(username,name,request)" +
                        "values('" + username + "','" + name + "','" + request + "')");
                if (i > 0) {
                    DBconn.closeConn();
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBconn.closeConn();
        return false;
    }

    @Override
    public boolean addfriend(String username,String name) {
        try {
            DBconn.init();
            ResultSet rs = DBconn.selectSql("select * from friend where username='" + username +
                    "' and name='" + name + "'");
            if (rs.next()) {
                DBconn.closeConn();
                return false;
            } else {
                int i = DBconn.addUpdDel("insert into friend(username,name) " +
                        "values('" + username + "','" + name + "')");
                if (i > 0) {
                    i = DBconn.addUpdDel("insert into friend(name,username) " +
                            "values('" + username + "','" + name + "')");
                    if (i > 0) {
                        DBconn.closeConn();
                        return true;
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBconn.closeConn();
        return false;
    }

    @Override
    public boolean delfriend(String username,String name) {
        boolean flag = false;
        DBconn.init();
        String sql = "delete from friend where (username = '" + username + "') and (name = '" + name + "')";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean deleterequest(String name,String request) {
        boolean flag = false;
        DBconn.init();
        String sql = "delete from addfriendrequest where (request = '" + request + "') and (name = '" + name + "')";
        int i = DBconn.addUpdDel(sql);
        System.out.println(i);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public List<User> queryfriends(String username) {
        List<User> list = new ArrayList<>();
        try {
            DBconn.init();
            String sql = "select name from friend where (username = '" + username + "')";
            ResultSet rs = DBconn.selectSql(sql);
            while (rs.next()) {
                List<User> friend = selectUser(0,rs.getString("name"),null,null);
                list.add(friend.get(0));
            }
            DBconn.closeConn();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> queryfriendrequests(String username) {
        List<User> list = new ArrayList<>();
        try {
            DBconn.init();
            String sql = "select username,request from addfriendrequest where (name = '" + username + "')";
            ResultSet rs = DBconn.selectSql(sql);
            while (rs.next()) {
                List<User> friend = selectUser(0,rs.getString("username"),null,null);
                friend.get(0).setAddfriendrequest(rs.getString("request"));
                list.add(friend.get(0));
            }
            DBconn.closeConn();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
